<template>
  <view class="container">
    <view class="header">
      <text class="title">编辑生活目标</text>
      <text class="subtitle">修改生活目标信息</text>
    </view>
    
    <Card class="form-card">
      <view class="form-group">
        <text class="label">目标标题</text>
        <input 
          class="input" 
          type="text" 
          placeholder="请输入生活目标，如：改正说谎毛病" 
          v-model="form.title"
        />
      </view>
      
      <view class="form-group">
        <text class="label">关联学生</text>
        <picker 
          mode="selector" 
          :range="students" 
          @change="onStudentChange"
        >
          <view class="picker">
            {{ selectedStudent || '请选择关联学生' }}
          </view>
        </picker>
      </view>
      
      <view class="form-group">
        <text class="label">目标达成标准</text>
        <textarea 
          class="textarea" 
          placeholder="请描述如何判断目标是否达成，例如：连续一周没有说谎" 
          v-model="form.criteria"
        />
      </view>
      
      <view class="form-group">
        <text class="label">目标描述</text>
        <textarea 
          class="textarea" 
          placeholder="请输入目标的详细描述（可选）" 
          v-model="form.description"
        />
      </view>
      
      <view class="form-group">
        <text class="label">期望完成时间</text>
        <picker 
          mode="date" 
          :value="form.targetDate" 
          @change="onTargetDateChange"
        >
          <view class="picker">
            {{ form.targetDate || '请选择期望完成时间' }}
          </view>
        </picker>
      </view>
      
      <Button 
        class="submit-btn" 
        type="primary" 
        text="保存修改" 
        :loading="loading"
        @click="handleSubmit"
      />
    </Card>
  </view>
</template>

<script>
export default {
  data() {
    return {
      form: {
        title: '改正写字潦草',
        student: '张小明 - 小学五年级',
        criteria: '连续一周作业字迹工整，老师无批评',
        description: '帮助张小明改善写字潦草的问题，提高作业质量。',
        targetDate: '2025-10-25'
      },
      students: ['张小明 - 小学五年级', '李小红 - 初中一年级', '王小刚 - 高中二年级'],
      selectedStudent: '张小明 - 小学五年级',
      loading: false
    };
  },
  
  methods: {
    onStudentChange(e) {
      this.selectedStudent = this.students[e.detail.value];
      this.form.student = this.selectedStudent;
    },
    
    onTargetDateChange(e) {
      this.form.targetDate = e.detail.value;
    },
    
    handleSubmit() {
      if (!this.form.title) {
        uni.showToast({
          title: '请输入目标标题',
          icon: 'none'
        });
        return;
      }
      
      if (!this.form.student) {
        uni.showToast({
          title: '请选择关联学生',
          icon: 'none'
        });
        return;
      }
      
      if (!this.form.criteria) {
        uni.showToast({
          title: '请输入目标达成标准',
          icon: 'none'
        });
        return;
      }
      
      // 模拟保存过程
      this.loading = true;
      setTimeout(() => {
        this.loading = false;
        uni.showToast({
          title: '保存成功',
          icon: 'success'
        });
        
        // 返回目标详情
        setTimeout(() => {
          uni.navigateBack();
        }, 1000);
      }, 1000);
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  padding: $spacing-base;
  background-color: $background-color;
  min-height: 100vh;
}

.header {
  text-align: center;
  margin-bottom: $spacing-lg;
  padding: $spacing-xl 0;
}

.title {
  font-size: $font-size-xl;
  font-weight: bold;
  color: $text-primary;
  display: block;
  margin-bottom: $spacing-sm;
}

.subtitle {
  font-size: $font-size-sm;
  color: $text-secondary;
}

.form-card {
  margin-bottom: $spacing-lg;
}

.form-group {
  margin-bottom: $spacing-lg;
}

.label {
  display: block;
  font-size: $font-size-base;
  color: $text-primary;
  margin-bottom: $spacing-sm;
}

.input, .picker, .textarea {
  width: 100%;
  padding: $spacing-base;
  border: 2rpx solid $text-tertiary;
  border-radius: $border-radius-base;
  font-size: $font-size-base;
  box-sizing: border-box;
}

.input {
  height: 100rpx;
}

.picker {
  height: 80rpx;
  line-height: 80rpx;
  display: flex;
  align-items: center;

}

.textarea {
  height: auto;
  min-height: 120rpx;
}

.submit-btn {
  width: inherit;
  
}
</style>